# -*- coding: utf-8 -*-
"""
Created on Thu Oct 14 15:06:59 2021

@author: lenovo
"""
from skimage import data,filters
from matplotlib import pyplot as plt
import numpy as np
#图像空间滤波函数
def corre12d(img,window):
    m=window.shape[0]
    n=window.shape[1]
    #边界通过0灰度值填充扩展
    img1=np.zeros((img.shape[0]+m-1,img.shape[1]+n-1))
    img1[(m-1)//2:(img.shape[0]+(m-1)//2),(n-1)//2:(img.shape[1]+(n-1)//2)]=img
    img2=np.zeros(img.shape)
    for i in range(img2.shape[0]):
        for j in range(img2.shape[1]):
            temp=img1[i:i+m,j:j+n]
            img2[i,j]=np.sum(np.multiply(temp,window))
            return img2
        #img为原始图像
        img=data.coffee()
        window=np.array([[0,0,2,0,0],[0,4,0,4,0],[2,0,-24,0,2],[0,4,0,4,0],[0,0,2,0,0]])
        img_laplace=corre12d(img,window)
        img_laplace=255*(img_laplace-img_laplace.min())/(img_laplace.max()-img_laplace.min())
       # img_laplace_enhance=img+img_laplace
        img_sobel=filters.sobel(img)
        plt.figure()
        plt.axis('off')
        plt.imshow(img,cmap='gray')
        plt.figure()
        plt.axis('off')
        plt.imshow(img_laplace,cmap='gray')
        plt.figure()
        plt.axis('off')
        plt.imshow(img_sobel,cmap='gray')
    
